package com.baimifan.renovation.publicfrag;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baimifan.renovation.R;
import com.baimifan.renovation.adapter.AddressFragAdapter;
import com.flyco.tablayout.SlidingTabLayout;

public class DialogFragmentWindow extends DialogFragment implements View.OnClickListener{
    private Float w, h, widthPixels, heightPixels;
    private ViewPager viewPager;
    private LinearLayout location;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog_fragment_window, container, false);
        getDisplay();
        init(view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(R.color.defalut_color);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = (int)(h*364);
        getDialog().getWindow().setAttributes(lp);;
    }

    private void init (View popView) {
        location = (LinearLayout) popView.findViewById(R.id.popup_location);
        TextView popup_address = (TextView) popView.findViewById(R.id.popup_address);
        View popup_view = popView.findViewById(R.id.popup_view);
        ImageView popup_iv = (ImageView) popView.findViewById(R.id.popup_iv);
        TextView popup_tag = (TextView) popView.findViewById(R.id.popup_tag);
        View popup_view2 = popView.findViewById(R.id.popup_view2);
        viewPager = (ViewPager) popView.findViewById(R.id.popup_viewpager);
        SlidingTabLayout popup_tab = (SlidingTabLayout) popView.findViewById(R.id.popup_tab);
        LinearLayout.LayoutParams params_address = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params_address.setMargins((int)(w*15), (int)(h*15), 0, (int)(h*15));
        popup_address.setLayoutParams(params_address);

        LinearLayout.LayoutParams params_view = new LinearLayout.LayoutParams(3,(int)(h*61));
        params_view.setMargins(0, (int)(h*16), 0, 0);
        popup_view.setLayoutParams(params_view);

        LinearLayout.LayoutParams params_iv = new LinearLayout.LayoutParams((int)(w*29.9),(int)(h*30));
        params_iv.setMargins((int)(w*30), (int)(h*18), (int)(w*31.1), (int)(h*5));
        popup_iv.setLayoutParams(params_iv);

        LinearLayout.LayoutParams params_tag= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params_tag.setMargins((int)(w*20), 0, 0, (int)(h*14));
        popup_tag.setLayoutParams(params_tag);

        LinearLayout.LayoutParams params_tab = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, (int)(h*38));
        params_tab.setMargins(0, 0, 0, 0);
        popup_tab.setLayoutParams(params_tab);

        LinearLayout.LayoutParams params_view2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                3);
        params_view2.setMargins(0, 0, 0, 0);
        popup_view2.setLayoutParams(params_view2);

        popup_tab.setBackgroundColor(Color.parseColor("#FFFFFF"));
        popup_tab.setTextSelectColor(Color.parseColor("#333333"));
        popup_tab.setTextUnselectColor(Color.parseColor("#999999"));
        popup_tab.setTabPadding(15);
        popup_tab.setTextsize(16);
        popup_tab.setIndicatorHeight(2);
        popup_tab.setIndicatorWidth(34);
        popup_tab.setIndicatorColor(Color.parseColor("#333333"));

        AddressFragAdapter addressFragAdapter = new AddressFragAdapter(getChildFragmentManager());
        viewPager.setAdapter(addressFragAdapter);
        popup_tab.setViewPager(viewPager);

        location.setOnClickListener(this);
    }

    public void current () {
        viewPager.setCurrentItem(1);
    }

    public void colseFrag () {
        dismiss();
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.popup_location:
                dismiss();
                break;
        }
    }
}
